package org.ww.edu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.ww.edu.bean.Video;
import org.ww.edu.client.VodClient;
import org.ww.edu.mapper.VideoMapper;
import org.ww.edu.service.VideoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程视频 服务实现类
 * </p>
 *
 * @author ww
 * @since 2020-10-23
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, Video> implements VideoService {

    @Autowired
    private VodClient vodClient;

    @Override
    public void deleteByCourseId(String courseId) {
        //根据课程id批量删除视频
        //先根据课程id找出所有的小节，根据小节获取视频id
        QueryWrapper<Video> videoQuery = new QueryWrapper<>();
        videoQuery.eq("course_id",courseId);
        videoQuery.select("video_source_id");
        List<Video> videos = baseMapper.selectList(videoQuery);
        List<String> videoList=new ArrayList<>();
        //将video中的videoSourceId全部取出来
        for(int i=0;i<videos.size();i++){
            Video video = videos.get(i);
            if(!StringUtils.isEmpty(video.getVideoSourceId())){
                videoList.add(video.getVideoSourceId());
            }
        }
        //判断videoList集合是否为空
        if(videoList.size()>0){
            vodClient.deleteBatch(videoList);
        }
        //删除小节信息
        QueryWrapper wrapper=new QueryWrapper();
        wrapper.eq("course_id",courseId);
        baseMapper.delete(wrapper);
    }
}
